//
//  BaseClassController.m
//  PackDemo
//
//  Created by 01 on 16/11/4.
//  Copyright © 2016年 蔡正军. All rights reserved.

#import "BaseClassController.h"
#import "HdMsgBox.h"
#import "NSURLProtocol+KYKJ.h"

@interface BaseClassController ()

@property (nonatomic,assign)  BOOL isPresentSheet;

@end

@implementation BaseClassController

- (void)viewDidLoad {
    [NSURLProtocol ky_registerScheme:@"http"];
    [NSURLProtocol ky_registerScheme:@"https"];
    
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [self resignFirstResponder];
}

// 子类可重写左上角 back返回
- (void)backNavItemTapped
{
    [self.navigationController popViewControllerAnimated:YES];
}
// 文本提示
- (void)presentSheet:(NSString *)title
{
    self.isPresentSheet = YES;
    [[HdMsgBox sharedInstance] showHUDTextOnly:title duration:3 inView:self.view];
}
// 懒加载
- (void)displayOverFlowActivityView
{
    self.isPresentSheet = NO;
//    [[HdMsgBox sharedInstance] showHUDLoading:self.view];
}
// 懒加载及提示消失
- (void)removeOverFlowActivityView
{
//    [[HdMsgBox sharedInstance] hideAllHUDsInView:self.view];
}
// 触屏手势 关闭提示
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    if (self.isPresentSheet)
    {
        [self removeOverFlowActivityView];
    }
}

/**
 *  成为第一响应者
 */
- (BOOL)canBecomeFirstResponder {//默认是NO，所以得重写此方法，设成YES
    return YES;
}

/**
 *  检测到摇动开始
 */
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (motion == UIEventSubtypeMotionShake) {
        NSLog(@"开始摇一摇");
    }
}

/**
 *  取消摇动
 */
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    //摇动取消
    NSLog(@"摇一摇取消");
}

/**
 *  点击分享
 */
- (void)onClickSocial:(NSArray *)activityItems
{
    // 服务类型控制器
    UIActivityViewController *activityViewController =
    [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
    
    [self presentViewController:activityViewController animated:YES completion:nil];
    
    // 选中分享类型
    [activityViewController setCompletionWithItemsHandler:^(NSString * __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError){
        // 显示选中的分享类型
        NSLog(@"act type %@",activityType);
        if (completed) {
            NSLog(@"ok");
        } else {
            NSLog(@"no ok");
        }
    }];
}
/**
 *  截屏
 *
 *  @param view 截屏的view
 *
 *  @return 返回一张image
 */
- (UIImage *)captureImageFromView:(UIView *)view
{
    CGRect screenRect = [view bounds];
    UIGraphicsBeginImageContext(screenRect.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:ctx];
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end
